// lib/widgets/community_list_widget.dart
import 'package:flutter/material.dart';
import '../../../models/community.dart';

class CommunityListWidget extends StatelessWidget {
  final List<Community> communities;
  final Function(int) onCommunityTap; // 修改为 int 类型
  final int selectedCommunityId; // 修改为 int 类型，0 表示全部

  const CommunityListWidget({
    super.key,
    required this.communities,
    required this.onCommunityTap,
    required this.selectedCommunityId, // 改为 required
  });

  @override
  Widget build(BuildContext context) {
    if (communities.isEmpty) {
      return const Center(child: Text('暂无社区数据'));
    }

    // 保留“全部”社区选项，ID设置为0
    List<Community> displayCommunities = [
      Community(communityId: 0, communityName: '全部'),
      ...communities,
    ];

    return SizedBox(
      height: 60,
      child: ListView.builder(
        scrollDirection: Axis.horizontal,
        itemCount: displayCommunities.length,
        itemBuilder: (context, index) {
          final community = displayCommunities[index];
          final bool isSelected = community.communityId == selectedCommunityId;

          return Padding(
            padding: const EdgeInsets.symmetric(horizontal: 8.0),
            child: GestureDetector(
              onTap: () => onCommunityTap(community.communityId ?? 0),
              child: Container(
                padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(20), // 胶囊形状
                  color: isSelected
                      ? Colors.red.withOpacity(0.1) // 选中时浅红色背景
                      : Colors.grey.withOpacity(0.1),
                  border: Border.all(
                    color: isSelected ? Colors.red : Colors.grey,
                  ),
                ),
                child: Text(
                  community.communityName ?? '未知社区',
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: isSelected ? FontWeight.bold : FontWeight.normal,
                    color: isSelected ? Colors.red : Colors.black87,
                  ),
                ),
              ),
            ),
          );
        },
      ),
    );
  }
}